//
//  TSPresentTransition.m
//  自定义转场动画
//
//  Created by 唐铁山 on 2018/9/12.
//  Copyright © 2018年 唐铁山. All rights reserved.
//

#import "TSPresentTransition.h"

@interface TSPresentTransition()

@end

@implementation TSPresentTransition
- (NSTimeInterval)transitionDuration:(nullable id <UIViewControllerContextTransitioning>)transitionContext {
    return 1.3f;
}

- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext {
    UIViewController *fromVc = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIViewController *toVc = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];

    CGRect screenBounds = [[UIScreen mainScreen] bounds];
    CGRect finalRect = [transitionContext finalFrameForViewController:toVc];

    toVc.view.frame = CGRectOffset(finalRect, 0, screenBounds.size.height);

    UIView *containerView = [transitionContext containerView];
    [containerView addSubview:fromVc.view];
    [containerView addSubview:toVc.view];
    // 添加动画
    NSTimeInterval duration = [self transitionDuration:transitionContext];
    [UIView animateWithDuration:0.5f animations:^{
        fromVc.view.transform = CGAffineTransformMakeScale(0.9f, 0.9f);
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:duration - 0.5f delay:0 usingSpringWithDamping:0.7f initialSpringVelocity:1.0 options:(UIViewAnimationOptionCurveLinear) animations:^{
            toVc.view.frame = finalRect;
        } completion:^(BOOL finished) {
            fromVc.view.layer.transform = CATransform3DIdentity;
            [transitionContext completeTransition:YES];
        }];
    }];
    
}
@end
